package com.epoint.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.epoint.webapp.dao.BlogDAO;
import com.epoint.webapp.dao.BlogMessageDAO;
import com.epoint.webapp.entity.Blog;
import com.epoint.webapp.entity.BlogMessage;
import com.epoint.webapp.entity.Member;
import com.epoint.webapp.util.PageCount;
import com.google.gson.Gson;

@Controller
public class BlogMessageController {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/deleteBlogMessage", method = RequestMethod.POST)
	public ModelAndView deleteBlogMessage(HttpServletRequest request, HttpSession session, int id) throws IOException {
		ModelAndView model = new ModelAndView();
		Member memberLogin = (Member)session.getAttribute("loginMember");
		if(memberLogin != null){
			BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
			BlogMessage blogMessage = new BlogMessage();
			blogMessage.setId(id);
			blogMessage = blogMessageDAO.get(blogMessage);
			if(memberLogin.getAccount().equals(blogMessage.getAccount())){
				blogMessageDAO.delete(blogMessage);
			}
			model.setViewName("redirect:"+request.getHeader("referer")+"#messageArea");	
		}
		else{
			model.setViewName("redirect:/login");
		}
		
		return model;
	}
	
	@RequestMapping(value = "/insertBlogMessage", method = RequestMethod.POST)
	public ModelAndView insertBlogMessage(HttpServletRequest request, HttpSession session, BlogMessage blogMessage) throws IOException {
		ModelAndView model = new ModelAndView();		
		Member memberLogin = (Member)session.getAttribute("loginMember");
		if(memberLogin != null){
			BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
			blogMessage.setAccount(memberLogin.getAccount());
			blogMessageDAO.insert(blogMessage);
			model.setViewName("redirect:"+request.getHeader("referer")+"#messageArea");
		}
		else{
			model.setViewName("redirect:/login");
		}
		return model;
	}
	
	@RequestMapping(value = "/management/manageBlogMessage", method = RequestMethod.GET)
	public ModelAndView manageBlogMessage(HttpServletRequest request, HttpSession session, int bid) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
		
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		int pageSize = 10;
		int count = blogMessageDAO.countTotal(bid);
		String pageUrl = "manageBlogMessage";
		String pageEndUrl = "&bid="+bid;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl);
		pageCount.setEndUrl(pageEndUrl);
		pageCount.calculate();
		
		List<BlogMessage> blogMessageList = new ArrayList<BlogMessage>();
		blogMessageList = blogMessageDAO.getList(bid, pageCount.getStart(), pageSize);
		model.addObject("blogMessageList", blogMessageList);
		model.addObject("blogMessageList2", new Gson().toJson(blogMessageList));
		model.addObject("page", pageCount);
		model.setViewName("management/manageBlogMessage");
		return model;
	}
	
	@RequestMapping(value = "/management/updateBlogMessageResponse", method = RequestMethod.POST)
	public ModelAndView updateBlogMessageResponse(HttpServletRequest request, HttpSession session, BlogMessage blogMessage) throws IOException {
		ModelAndView model = new ModelAndView();		
		BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
		blogMessageDAO.updateResponse(blogMessage);
		model.setViewName("redirect:"+request.getHeader("referer"));
		return model;
	}
	
	@RequestMapping(value = "/management/deleteBlogMessage", method = RequestMethod.POST)
	public ModelAndView deleteBlogMessageByManager(HttpServletRequest request, HttpSession session, int id) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
		BlogMessage blogMessage = new BlogMessage();
		blogMessage.setId(id);
		blogMessageDAO.delete(blogMessage);
		model.setViewName("redirect:"+request.getHeader("referer"));		
		return model;
	}
}
